package com.btpj.hospital_server.service;

import com.btpj.hospital_server.dto.param.HospitalOutpatientParam;
import com.btpj.hospital_server.entity.HospitalOutpatient;
import com.btpj.hospital_server.mapper.HospitalOutpatientMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 医院门诊Service
 *
 * @author BTPJ  2020/8/26
 */
@Service
public class HospitalOutpatientService {

    @Resource
    private HospitalOutpatientMapper hospitalOutpatientMapper;

    @Resource
    private HospitalInfoService hospitalInfoService;

    /**
     * 是否存在指定ID的门诊
     *
     * @param outPatientId 门诊的编号
     * @return 是否存在指定ID的门诊
     */
    public boolean existOutpatient(Long outPatientId) {
        return hospitalOutpatientMapper.selectByPrimaryKey(outPatientId) != null;
    }

    /**
     * 是否存在指定名称的门诊
     *
     * @param name 门诊的名称
     * @return 是否存在指定名称的门诊
     */
    public boolean existOutpatient(String name) {
        return hospitalOutpatientMapper.selectByName(name) != null;
    }

    /**
     * 获取指定ID的门诊的名称
     *
     * @param id 门诊的编号
     * @return 门诊的名称
     */
    public String getName(Long id) {
        HospitalOutpatient hospitalOutpatient = hospitalOutpatientMapper.selectByPrimaryKey(id);
        return hospitalOutpatient != null ? hospitalOutpatient.getName() : null;
    }

    /**
     * 获取指定专科下的门诊列表
     *
     * @param specialId 专科编号
     * @return 指定专科下的门诊列表
     */
    public List<HospitalOutpatient> getListBySpecialId(Long specialId) {
        return hospitalOutpatientMapper.selectBySpecialId(specialId);
    }

    /**
     * 添加门诊信息
     *
     * @param param HospitalOutpatientParam
     * @return 是否添加成功
     */
    public boolean addOutpatient(HospitalOutpatientParam param) {
        HospitalOutpatient hospitalOutpatient = new HospitalOutpatient();
        BeanUtils.copyProperties(param, hospitalOutpatient);
        hospitalOutpatient.setGmtCreate(new Date());
        hospitalOutpatient.setGmtModified(new Date());
        return hospitalOutpatientMapper.insertSelective(hospitalOutpatient) > 0;
    }

    /**
     * 修改门诊信息
     *
     * @param id    门诊编号
     * @param param HospitalOutpatientParam
     * @return 是否修改成功
     */
    public boolean updateOutpatient(Long id, HospitalOutpatientParam param) {
        HospitalOutpatient outpatient = hospitalOutpatientMapper.selectByPrimaryKey(id);
        BeanUtils.copyProperties(param, outpatient);
        outpatient.setGmtModified(new Date());
        return hospitalOutpatientMapper.updateByPrimaryKeySelective(outpatient) > 0;
    }

    /**
     * 删除门诊信息
     *
     * @param id 门诊编号
     * @return 是否删除成功
     */
    public boolean deleteOutpatient(Long id) {
        return hospitalOutpatientMapper.deleteByPrimaryKey(id) > 0;
    }

    /**
     * 获取门诊信息
     *
     * @param id 门诊编号
     * @return 门诊信息
     */
    public HospitalOutpatient getOutpatientDetails(Long id) {
        return hospitalOutpatientMapper.selectByPrimaryKey(id);
    }

    /**
     * 获取门诊列表
     *
     * @param name       门诊名称（筛选条件）
     * @param hospitalId 所属医院编号（筛选条件）
     * @param specialId  所属专科编号（筛选条件）
     * @return 门诊列表
     */
    public List<HospitalOutpatient> getList(String name, Long hospitalId, Long specialId) {
        List<Long> hospitalOutpatientIds = null;
        if (hospitalId != null) {
            hospitalOutpatientIds = hospitalInfoService.getOutpatientIdListByHospitalId(hospitalId);
            if (hospitalOutpatientIds.size() == 0) {
                // 表示此医院编号下无门诊或医院编号不存在，直接返回空集合
                return Collections.emptyList();
            }
        }

        return hospitalOutpatientMapper.selectByNameOrSpecialIdAndIdIn(name, specialId, hospitalOutpatientIds);
    }

}
